var app = getApp();
Page({
  data: {
    testdata:[], //服务集市列表
    id:null,
    motto: 'Hello World',
    userInfo: {},
    openid:null,
    url:null
  },
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
    this.loadPage(this.data.id)
    var _this=this;
     wx.login({
      success: function(res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://wx.ichelaba.com/chelabaapi/index.php/SmallProgramLogin',
            data: {
              code: res.code
            },
            success:function(a){
               _this.data.openid=a.data.openid
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
  },
  loadPage: function (options) { //加载数据渲染页面
    var _this=this;
    wx.request({
      url: 'https://wx.ichelaba.com/chelabaapi/index.php/selectpromotion/ajaxDetailForWx',//上线的话必须是https，没有appId的本地请求貌似不受影响
      data: {"id":this.data.id},
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        const newlist = [];
          newlist.push({
            "link":res.data.detail.link,
            "con":res.data.detail.content_img_list,
            "id":1,
            "src":res.data.detail.pic,
            "title":res.data.detail.promotion_title,
            "main":[res.data.detail.pic2,res.data.detail.pic3,res.data.detail.pic4],
            "promotion_info1":res.data.detail.promotion_info,
            "promotion_info2":res.data.detail.promotion_info2,
            "promotion_info3":res.data.detail.promotion_info3,
            "cheapExchange_time":res.data.detail.source+" "+res.data.detail.time
          })
          _this.setData({
            url:res.data.detail.link
          });
          _this.setData({
            testdata:_this.data.testdata.concat(newlist)
          });
      },
      fail:function(){
      },
      complete: function(){
      }
    });
  },formSubmit:function(e) {
    var _this=this;
     // console.log('form发生了submit事件，携带数据为：', e.detail.value.formid)
    wx.request({
      url: 'https://wx.ichelaba.com/chelabaapi/index.php/SmallProgramTemplate',
      data: {
        openid:_this.data.openid,
        url:_this.data.url,
        formid:e.detail.formId
      },
      success:function(a){
        console.log(a)
      }
    })
  },
})